package cn.hzw.doodledemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.Toast;

import cn.forward.androids.ScaleGestureDetectorApi27;
import cn.hzw.doodle.BaseDoodleOnTouchGestureListener;
import cn.hzw.doodle.DoodleColor;
import cn.hzw.doodle.DoodleOnTouchGestureListener;
import cn.hzw.doodle.DoodlePen;
import cn.hzw.doodle.DoodleRectOnTouchGestureListener;
import cn.hzw.doodle.DoodleRotatableItemBase;
import cn.hzw.doodle.DoodleShape;
import cn.hzw.doodle.DoodleTouchDetector;
import cn.hzw.doodle.DoodleView;
import cn.hzw.doodle.IDoodleListener;
import cn.hzw.doodle.core.IDoodle;
import cn.hzw.doodle.core.IDoodleItem;
import cn.hzw.doodle.core.IDoodleSelectableItem;

/**
 * Change text's size by scale gesture
 * 通过手势缩放item
 */
public class ScaleGestureItemDemo extends Activity {
    private DoodleView doodleView;
    private BaseDoodleOnTouchGestureListener touchGestureListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scale_gesture_item);

        // step 1
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.thelittleprince2);
        doodleView = new DoodleView(this, bitmap, new IDoodleListener() {
            @Override
            public void onSaved(IDoodle doodle, Bitmap doodleBitmap, Runnable callback) {
                Toast.makeText(ScaleGestureItemDemo.this, "onSaved", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onReady(IDoodle doodle) {
                doodle.setSize(40 * doodle.getUnitSize());
                doodle.setPen(DoodlePen.BRUSH);
                doodle.setShape(DoodleShape.HOLLOW_RECT);
                doodle.setColor(new DoodleColor(Color.RED));
                final Rect rectD = new Rect(0,0,200,200);
                IDoodleSelectableItem item1 = new DoodleRotatableItemBase(doodle,0,0,0) {

                    @Override
                    public void setScale(float scale) {
                        super.setScale(scale);
                        Log.e(getClass().getSimpleName(),"scale->"+scale);
                        rectD.top*=scale;
                        rectD.left*=scale;
                        rectD.right*=scale;
                        rectD.bottom*=scale;
                    }

                    @Override
                    protected void resetBounds(Rect rect) {

                        rect.set(rectD);
                        rect.top = (int) (rect.top-getSize());
                        rect.left = (int) (rect.left-getSize());
                        rect.right = (int) (rect.right+getSize());
                        rect.bottom = (int) (rect.bottom+getSize());
                    }

                    @Override
                    protected void doDraw(Canvas canvas) {
                        Paint paint = new Paint();
                        paint.setColor(Color.BLUE);
                        paint.setStyle(Paint.Style.STROKE);
                        paint.setStrokeWidth(getSize());
                        canvas.drawRect(rectD,paint);
                        Log.e(getClass().getSimpleName(),rectD.toString());


                    }
                };
                item1.setShape(DoodleShape.HOLLOW_RECT);
                item1.setSize(10);
                item1.setLocation(doodle.getBitmap().getWidth()/2,doodle.getBitmap().getHeight()/2);
                item1.setSelected(true);
                touchGestureListener.setSelectedItem(item1);
                doodle.addItem(item1);

            }
        });

        // step 2
        touchGestureListener = new DoodleRectOnTouchGestureListener(doodleView, new BaseDoodleOnTouchGestureListener.ISelectionListener() {



            @Override
            public void onSelectedItem(IDoodle doodle, IDoodleSelectableItem selectableItem, boolean selected) {

                Log.e("xiong","onSelectedItem:"+selected);
            }

            @Override
            public void onCreateSelectableItem(final IDoodle doodle, final float x, final float y) {
                Log.e("xiong","onCreateSelectableItem");

            }
        });

        touchGestureListener.setSupportScaleItem(true);
        DoodleTouchDetector touchDetector = new DoodleTouchDetector(this, touchGestureListener);
        doodleView.setDefaultTouchDetector(touchDetector);
        doodleView.setEditMode(true);

        // step 4
        ViewGroup container = (ViewGroup) findViewById(R.id.doodle_container);
        container.addView(doodleView);
    }

    /**
     * Change selected item's size by scale gesture.
     * Note that this class extends DoodleOnTouchGestureListener and does not affect the original gesture logic.
     * <p>
     * 注意，这里继承了DoodleOnTouchGestureListener，不影响原有的手势逻辑
     */


}
